package com.heima.controller;

import com.heima.annotation.GlobalInterceptor;
import com.heima.annotation.VerifyParam;
import com.heima.component.RedisComponent;
import com.heima.entity.constants.Constants;
import com.heima.entity.dto.SessionWebUserDto;
import com.heima.entity.dto.SysSettingsDto;
import com.heima.entity.enums.ShareValidTypeEnums;
import com.heima.entity.po.FileInfo;
import com.heima.entity.po.FileShare;
import com.heima.entity.po.UserInfo;
import com.heima.entity.query.FileInfoQuery;
import com.heima.entity.query.FileShareQuery;
import com.heima.entity.query.UserInfoQuery;
import com.heima.entity.vo.PaginationResultVO;
import com.heima.entity.vo.ResponseVO;
import com.heima.entity.vo.UserInfoVO;
import com.heima.service.FileInfoService;
import com.heima.service.FileShareService;
import com.heima.service.RecycleService;
import com.heima.service.UserInfoService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController extends CommonFileController{
    @Resource
    private FileInfoService fileInfoService;

    @Resource
    private RedisComponent redisComponent;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RecycleService recycleService;
    @RequestMapping("/getSysSettings")
    @GlobalInterceptor(checkParams = true,checkAdmin = true)
    public ResponseVO getSysSettings(){
        return getSuccessResponseVO(redisComponent.getsysSettingsDto());
    }

    @RequestMapping("/saveSysSettings")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public ResponseVO saveSysSettings(
            @VerifyParam(required = true) String registerEmailTitle,
            @VerifyParam(required = true) String registerEmailContent,
            @VerifyParam(required = true) Integer userInitUseSpace) {
        SysSettingsDto sysSettingsDto = new SysSettingsDto();
        sysSettingsDto.setRegisterEmailTitle(registerEmailTitle);
        sysSettingsDto.setRegisterEmailContent(registerEmailContent);
        sysSettingsDto.setUserInitUseSpace(userInitUseSpace);
        redisComponent.saveSysSettingsDto(sysSettingsDto);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadUserList")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public ResponseVO loadUserList(UserInfoQuery userInfoQuery) {
        userInfoQuery.setOrderBy("join_time desc");
        PaginationResultVO<UserInfo> listByPage = userInfoService.findListByPage(userInfoQuery);
        return getSuccessResponseVO(convert2PaginationVO(listByPage, UserInfoVO.class));
    }

    @RequestMapping("/updateUserStatus")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public ResponseVO updateUserStatus(@VerifyParam(required = true) String userId,
                                       @VerifyParam(required = true) Integer status) {
        userInfoService.updateUserStatus(userId,status);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/updateUserSpace")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public ResponseVO updateuserSpace(@VerifyParam(required = true) String userId,
                                      Long changeSpace){
        Long space = changeSpace * Constants.MB;
        userInfoService.updateUserSpace(userId,space);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadFileList")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public ResponseVO loadFileList(FileInfoQuery fileInfoQuery){
        fileInfoQuery.setOrderBy("folder_type desc,last_update_time desc");
        fileInfoQuery.setQueryNickName(true);
        PaginationResultVO<FileInfo> listByPage = fileInfoService.findListByPage(fileInfoQuery);
        return getSuccessResponseVO(listByPage);
    }

    @RequestMapping("/getFolderInfo")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public ResponseVO getFolderInfo(String path){

        return super.getFolderInfo(path,null);
    }

    @RequestMapping("/getFile/{userId}/{fileId}")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public void getFile(@PathVariable("userId") String userId,@PathVariable("fileId") String fileId, HttpServletResponse response){

        super.getFile(response,fileId,userId);
    }

    @RequestMapping("/ts/getVideoInfo/{userId}/{fileId}")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public void getImage(@PathVariable("userId") String userId,@PathVariable("fileId") String fileId, HttpServletResponse response){

        super.getFile(response,fileId,userId);
    }

    @RequestMapping("/createDownloadUrl/{userId}/{fileId}")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public ResponseVO createDownloadUrl(@PathVariable("userId") @VerifyParam(required = true) String userId,
                                        @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
        return super.createDownloadUrl(fileId, userId);
    }

    /**
     * 下载
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/download/{code}")
    @GlobalInterceptor(checkLogin = false, checkParams = true)
    public void download(HttpServletRequest request, HttpServletResponse response,
                         @PathVariable("code") @VerifyParam(required = true) String code) throws Exception {
        super.download(request, response, code);
    }


    @RequestMapping("/delFile")
    @GlobalInterceptor(checkParams = true, checkAdmin = true)
    public ResponseVO delFile(@VerifyParam(required = true) String fileIdAndUserIds) {
        String[] fileIdAndUserIdArray = fileIdAndUserIds.split(",");
        for (String fileIdAndUserId : fileIdAndUserIdArray) {
            String[] itemArray = fileIdAndUserId.split("_");
            recycleService.delFile4ever(itemArray[0], itemArray[1]);
        }
        return getSuccessResponseVO(null);
    }
}
